﻿Imports TransportadoraToledoDAL

Public Class CombustivelBLL
    Dim objDAL As CombustivelDAL

#Region "propriedades"

    Private codigo As Integer
    Public Property pCodigo() As Integer
        Get
            Return codigo
        End Get
        Set(ByVal value As Integer)
            codigo = value
        End Set
    End Property

    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Private tipo As String
    Public Property ptipo() As String
        Get
            Return tipo
        End Get
        Set(ByVal value As String)
            tipo = value
        End Set
    End Property


   
#End Region

#Region "Métodos"
    Sub New()

    End Sub

    Sub New(ByVal codigo As Integer, ByVal tipo As String)
        Me.codigo = codigo
        Me.tipo = tipo
    End Sub
   

    Sub inserir()
        Try
            If (tipo = "") Then
                Throw New Exception("Preencha todos os campos!")
            Else
                'inserir
                objDAL = New CombustivelDAL
                Me.codigo = objDAL.inserir(tipo)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub alterar()
        Try
            If (codigo = 0 Or tipo = "") Then
                Throw New Exception("Preencha todos os campos!")
            Else
                'alterar
                objDAL = New CombustivelDAL
                objDAL.alterar(codigo, tipo)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub excluir()
        Try
            If (codigo = 0) Then
                Throw New Exception("Campo(s) inválido(s)!")
            Else
                'excluir
                objDAL = New CombustivelDAL
                objDAL.excluir(codigo)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    'retorna todas as linhas pesquisadas da tabela
    Function localizar(ByVal tipo As String) As DataTable
        Dim tab As DataTable = Nothing
        Try
            objDAL = New CombustivelDAL
            tab = objDAL.localizar(tipo)

        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function


    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal tipo As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Try
            If (tipo = "ultimo") Then
                objDAL = New CombustivelDAL
                tab = objDAL.localizar(tipo)
                If (tab.Rows(0)("com_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("com_codigo").ToString
                End If
            ElseIf (tipo = "primeiro") Then
                objDAL = New CombustivelDAL
                tab = objDAL.localizar(tipo)
                If (tab.Rows(0)("com_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("com_codigo").ToString
                End If
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Function localizarProxAnteriro(ByVal descricao As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try

            objDAL = New CombustivelDAL
            tab = objDAL.localizarProxAnterior(descricao, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("com_codigo").ToString
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function

    'localizar uma linha da tabela w preenche as propriedades
    Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New CombustivelDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)("com_codigo").ToString
                Me.tipo = tab.Rows(0)("com_tipo").ToString
                'limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Function localizarcombustivel(ByVal codigo As Integer) As DataTable
        Dim tab As DataTable
        Try
            objDAL = New CombustivelDAL
            tab = objDAL.localizar(codigo)
        Catch ex As Exception
            Throw ex
        End Try
        Return tab
    End Function

    Sub limpar()
        Me.codigo = Nothing
        Me.tipo = Nothing
    End Sub

    Sub insereCombustivel(ByVal codveiculo As Integer, ByVal codcombustivel As Integer)
        Try
            If (codveiculo = 0 Or codcombustivel = 0) Then
                Throw New Exception("Preencha todos os campos!")
            Else
                'alterar
                objDAL = New CombustivelDAL
                objDAL.insereCombustivel(codveiculo, codcombustivel)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub localizanewcombustivel(ByVal codveiculo As Integer)
        Dim tab As DataTable
        Try
            If (codveiculo > 0) Then
                objDAL = New CombustivelDAL
                tab = objDAL.localizacombustivel(codveiculo)
                If (tab.Rows.Count > 0) Then
                    Me.codigo = tab.Rows(0)("com_codigo").ToString
                    Me.tipo = tab.Rows(0)("com_tipo").ToString
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub
    Sub carrega(codigo As Integer)
        Dim tab As DataTable

        Try
            objDAL = New CombustivelDAL

            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)("com_codigo").ToString
                Me.tipo = tab.Rows(0)("com_tipo").ToString
            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try

    End Sub
#End Region
End Class
